<?php
declare (strict_types=1);

namespace app\admin\controller;

use addons\BuilderPlus\components\Button;
use addons\BuilderPlus\components\Column;
use addons\BuilderPlus\components\Search;
use addons\BuilderPlus\facade\Table;
use think\response\Redirect;

class SummaryUser extends Base
{
    public function getOptions(): array
    {
        return [
            'pk' => 'id',
            'model' => \app\common\model\SummaryUser::class,
            'with' => ['user']
        ];
    }

    public function index(): string
    {
        $table = Table::init($this)
            ->setCheckbox(false)
            ->addColumn('create_date', '日期', fn(Column $col) => $col->width(120)->totalRowText('合计'))
            ->addColumn('uid', '相关会员', fn(Column $col) => $col->templet('#_userTpl')->minWidth(240));
        foreach (getBonusTitle(2) as $k => $v) {
            $table->addColumn($k, $v, fn(Column $col) => $col->totalRow(true));
        }
        return $table
            ->addColumn('update_time', '更新时间')
            ->addRightButtons([
                'edit' => fn(Button $btn) => $btn->title('查看明细')
                    ->url(url('summaryInfo')->build())
                    ->width('90%')
            ])
            ->addSearch([
                ['create_time', '日期', fn(Search $search) => $search->type('date')],
                ['uid', '会员编号/昵称/手机号', fn(Search $search) => $search->relation('\app\common\model\User', 'code|nickname|mobile,id')->condition('Like')]
            ])
            ->setTotalRow(true)
            ->enableExport()
            ->fetch();
    }

    public function summaryInfo($id): Redirect
    {
        $info = $this->model::where(['id' => $id])->find();
        $create_time = $info->create_date . ' - ' . $info->create_date;
        $url = url('account/bonus') . '?create_time=' . $create_time . '&uid=' . $info->user->code;

        return redirect($url);
    }
}